# class Solution:
#     def threeSumClosest(self, nums, target):
#         nums.sort()
#         n = len(nums)
#         best = 10 ** 7
#         # 根据差值的绝对值来更新答案
#         def update(cur):
#             nonlocal best
#             if abs(cur - target) < abs(best - target):
#                 best = cur
#         # 枚举 a
#         for i in range(n):
#             # 保证和上一次枚举的元素不相等
#             if i > 0 and nums[i] == nums[i - 1]:
#                 continue
#             # 使用双指针枚举 b 和 c
#             j, k = i + 1, n - 1
#             while j < k:
#                 s = nums[i] + nums[j] + nums[k]
#                 # 如果和为 target 直接返回答案
#                 if s == target:
#                     return target
#                 update(s)
#                 if s > target:
#                     # 如果和大于 target，移动 c 对应的指针
#                     k0 = k - 1
#                     # 移动到下一个不相等的元素
#                     while j < k0 and nums[k0] == nums[k]:
#                         k0 -= 1
#                     k = k0
#                 else:
#                     # 如果和小于 target，移动 b 对应的指针
#                     j0 = j + 1
#                     # 移动到下一个不相等的元素
#                     while j0 < k and nums[j0] == nums[j]:
#                         j0 += 1
#                     j = j0
#
#         return best

class Solution:
    def threeSumClosest(self, nums, target):
        n = len(nums)
        res = 0
        minGap = -1
        nums.sort()
        print(nums)
        for i in range(n):
            print('i',i)
            L,R = i+1,n-1
            while L<R:
                diff = abs(nums[i]+nums[L]+nums[R]-target)
                print('diff',diff)
                sum=nums[i] + nums[L] + nums[R]
                print(sum)
                if minGap == -1 or diff < minGap:
                    minGap = diff
                    res = sum
                    print('minGap',minGap,'res' ,res)
                if nums[i] + nums[L] + nums[R] == target: return target


                if sum > target:
                    R -= 1
                    while L<R and nums[R] == nums[R+1]:
                        R -= 1
                else:
                    L += 1
                    while L < R and nums[L] == nums[L - 1]:
                        L += 1
        return res
nums = [-1,0,1,1,55]
target = 3
print(Solution().threeSumClosest(nums,target))
'''
debug经历:
1. 用绝对值判断值的变大变小,还要保存最小差距以及对应的数据之和并返回
2. 去重需要在判断完当前情况之后,应该出现在嵌套之中.需要确定移动的数值与当前不相等
3. 先判重效果更好
'''
class Solution:
    def threeSumClosest(self, nums: List[int], target: int) -> int:
        n = len(nums)
        res = 0
        maxGap = -1
        nums.sort()
        for i in range(n):
            L ,R= i+1,n-1
            while L<R:
                temp = nums[i]+nums[L]+nums[R]
                diff = abs(temp - target)
                if maxGap == -1 or diff < maxGap:
                    maxGap = diff
                    res = temp
                if diff == 0: return target
                if temp < target:
                    while L<R and nums[L] == nums[L+1]:
                        L+=1
                    L+=1
                else:
                    while L<R and nums[R] == nums[R-1]:
                        R-=1
                    R-=1
        return res